package com.seamtop.cuber.common.base;

import com.seamtop.cuber.common.entriy.CuberCounter;
import com.seamtop.cuber.common.entriy.ErrorCode;
import com.seamtop.cuber.common.exception.CuberFileParseException;
import com.seamtop.cuber.common.util.StringUtil;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;

/**
 * Created by feng on 2015/10/17.
 */
public class CuberLocalInit implements ICuberInit {

    private static final Logger LOG = LoggerFactory.getLogger(CuberLocalInit.class);

    public Document loadCuberCounter(String filePath) throws Exception{
        HashMap<String,CuberCounter> cuberCounterMap = new HashMap<String, CuberCounter>();
        String fileName = "/table-counter.xml";
        SAXReader saxReader = new SAXReader();
        InputStream is = null;
        if(!StringUtil.isEmpty(filePath)){
            is = new FileInputStream(new File(filePath + fileName));
        }else{
            is = CuberLocalInit.class.getResourceAsStream(fileName);
        }

        if(is == null){
            throw new CuberFileParseException(new ErrorCode(ErrorCode.FILE_LOAD_ERROR, fileName).toString());
        }
        Document document = saxReader.read(is);
        return document;
    }

    public Document loadTruthMData(String filePath) throws Exception {
        String fileName = "/table-schema.xml";
        SAXReader saxReader = new SAXReader();
        InputStream is = null;
        if(!StringUtil.isEmpty(filePath)){
            is = new FileInputStream(new File(filePath + fileName));
        }else{
            is = CuberLocalInit.class.getResourceAsStream(fileName);
        }
        if(is == null){
            throw new CuberFileParseException(new ErrorCode(ErrorCode.FILE_LOAD_ERROR, fileName).toString());
        }
        Document document = saxReader.read(is);
        return document;
    }

    public Document loadVirtualMData(String filePath) throws Exception {
        String fileName = "/table-virtuals.xml";
        SAXReader saxReader = new SAXReader();
        InputStream is = null;
        if(!StringUtil.isEmpty(filePath)){
            is = new FileInputStream(new File(filePath + fileName));
        }else{
            is = CuberLocalInit.class.getResourceAsStream(fileName);
        }
        if(is == null){
            throw new CuberFileParseException(new ErrorCode(ErrorCode.FILE_LOAD_ERROR, fileName).toString());
        }
        Document document = saxReader.read(is);
        return document;
    }

    public Document loadCuberReducer(String filePath) throws Exception {
        String fileName = "/table-reducer.xml";
        SAXReader saxReader = new SAXReader();
        InputStream is = null;
        if(!StringUtil.isEmpty(filePath)){
            is = new FileInputStream(new File(filePath + fileName));
        }else{
            is = CuberLocalInit.class.getResourceAsStream(fileName);
        }
        Document document = saxReader.read(is);
        return document;
    }
}
